const remote = require('electron').remote;
const BrowserWindow = remote.BrowserWindow;

var fs = require("fs");
// 在文件头部引入 Node.js 中的 path 模块
const path = require('path');

// 获取ipcMain对象
const ipcMain = remote.ipcMain;
const {ipcRenderer} = require('electron');
ipcMain.on('other',(event, str) => {
   const labelReturn = document.getElementById('label_return');
   labelReturn.innerText = labelReturn.innerText + '\r\n' + str;
});
//  主窗口向other窗口发送数据
function onClick_SendData() {
    var win = new BrowserWindow({
      show:false,
      x:10,
      y:20,
      width:400,
      height:400,
      webPreferences: {
        nodeIntegration: true,
        contextIsolation: false,
        enableRemoteModule: true,
      }
    });
    win.loadFile('./other.html');
    win.once('ready-to-show',()=>{
        win.show();
        win.webContents.send('data',{name:'Bill',salary:2345});
    });

}

//  other窗口装载页面时显示主窗口发过来的数据
function onLoad() {
    ipcRenderer.on('data',(event, obj) => {
       const labelName = document.getElementById('label_name');
       const labelSalary = document.getElementById('label_salary');
       labelName.innerText = obj.name;
       labelSalary.innerText = obj.salary;
    });
}

//  关闭other窗口

function onClick_Close() {
    const win = remote.getCurrentWindow();
    ipcRenderer.send('other','窗口已经关闭');
    win.close();
}

console.log("-----")
fs.readFile("mock/key.json",(err,data)=>{
    console.log(data)
})

window.onload = () => {
    var btnLoad  = document.querySelector("#btnLoadJson")
    var txtJson  = document.querySelector("#txtJson")
    var contentDom  = document.querySelector("#content")
    console.log(".....!")
    btnLoad.onclick = ()=>{
        console.log("clicked!")
        fs.readFile("mock/key.json",(err,data)=>{
            if(err) {
                console.log(err);
                return;
            }
            console.log(data);
            txtJson.innerHTML = data;
        })
    }

    btnAdd.onclick = ()=>{
        const mainWindow = new BrowserWindow({
            width:800,
            height: 600,
           webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
            enableRemoteModule: true,
          }
        })
        console.log(path.join(__dirname,'new.html'))
        //加载本地页面
        mainWindow.loadFile(path.join(__dirname,'new.html'))
    }

    contentDom.ondragenter = contentDom.ondragover = ()=>{
        console.log("contentDom.ondragover....");
        return false;
    }

    contentDom.ondrop =(e)=>{
        var filePath  = e.dataTransfer.files[0].path
        console.log(filePath)
        fs.readFile(filePath,(err,data)=>{
            if(err) {
                console.log(err);
                return;
            }
            console.log(data);
            txtJson.innerHTML = data;
        })
    }
}